package cc.messcat.web.base;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import cc.messcat.common.constant.CommonConstant;
import cc.messcat.common.model.FilesUploadModel;
import cc.messcat.common.model.ResponseBean;

@Controller
@RequestMapping("/fileUpload")
public class FileUploadController {

	@Value("#{ configProperties['web.name'] }")
	private String projectName;
	
	@Value("#{ configProperties['file.web.name'] }")
	private String fileProjectName;

	@Value("#{ configProperties['file.upload.image.path'] }")
	private String filePath;


	@RequestMapping(value = "/imageUpload", method = RequestMethod.POST)
	@ResponseBody
	public Object pullImageFile(MultipartFile file) throws Exception {
		if (!file.isEmpty()) {
			FilesUploadModel fileModel = new FilesUploadModel(file.getOriginalFilename(), file.getInputStream(), projectName, filePath);
			fileModel.writeFileByBinaryToDoMain(fileProjectName);
			if (!fileModel.isFileExists()) {
				throw new Exception("文件写入异常！！");
			}
			return new ResponseBean(CommonConstant.SUCCESS_CODE_200, CommonConstant.MSG_SUCCESS, fileModel.getFileUri());
		}else {
			return new ResponseBean(CommonConstant.FAIL_CODE_400,"文件不能为空");
		}
	}
}
